﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.Module.DataDetail
{
    public partial class WeekSumByCompany : BasePage
    {
        private QS_PLANWEEK_DETAIL_BLL weekBll = new QS_PLANWEEK_DETAIL_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FORM_DATE.Value = DateTime.Now.ToString("yyyy-MM");
                BindDataSet();
            }
        }

        public void BindDataSet()
        {
            if (FORM_COMPANY_ID.Value == -1)
            {
                //cmyId = CurrentUserCompanyID;
                MessageBox.Show(Page, "请选择公司");
                return;
            }
            if (FORM_DATE.Value == "")
            {
                MessageBox.Show(Page, "请选择业务日期");
                return;
            }
            var cmyId = FORM_COMPANY_ID.Value;
            var year = this.FORM_DATE.Value.Split('-')[0];
            var month = this.FORM_DATE.Value.Split('-')[1];
            var week = this.DropDownList3.Text.ToString();
            var cmyIds = new Sys_CompanyBLL().GetCompanyFatherAndChildByID(cmyId);

            #region datatable SQL语句

            string sql = string.Format(@"SELECT T1.D_START AS D_START,T1.D_END AS D_END,T1.N_YEAR AS N_YEAR,T1.N_MONTH AS N_MONTH,TT.* FROM (
	SELECT
		T4.N_COMPANY_ID AS N_COMPANY_ID,
		T5.CMY_NAME AS CMY_NAME,
		SUM(DECODE(T3.N_PROID,1,T3.N_AMOUNT,NULL)) AS SUM_PLAN,
		SUM(DECODE(T3.N_PROID,301,T3.N_AMOUNT,NULL)) AS SUM_ONE,
		SUM(DECODE(T3.N_PROID,302,T3.N_AMOUNT,NULL)) AS SUM_TWO,
		SUM(DECODE(T3.N_PROID,303,T3.N_AMOUNT,NULL)) AS SUM_THREE,
		SUM(DECODE(T3.N_PROID,304,T3.N_AMOUNT,NULL)) AS SUM_FOUR,
		SUM(DECODE(T3.N_PROID,305,T3.N_AMOUNT,NULL)) AS SUM_FIVE,
		SUM(DECODE(T3.N_PROID,306,T3.N_AMOUNT,NULL)) AS SUM_SIX,
		SUM(DECODE(T3.N_PROID,307,T3.N_AMOUNT,NULL)) AS SUM_SEVEN,
		SUM(DECODE(T3.N_PROID,308,T3.N_AMOUNT,NULL)) AS SUM_EIGHT,
		SUM(DECODE(T3.N_PROID,309,T3.N_AMOUNT,NULL)) AS SUM_NINE,
		SUM(DECODE(T3.N_PROID,310,T3.N_AMOUNT,NULL)) AS SUM_TEN,
		SUM(DECODE(T3.N_PROID,311,T3.N_AMOUNT,NULL)) AS SUM_ELEVEN,
		SUM(DECODE(T3.N_PROID,10,T3.N_AMOUNT,NULL)) AS SUM_TWELVE,
		SUM(DECODE(T3.N_PROID,11,T3.N_AMOUNT,NULL)) AS SUM_THIRTEEN,
		SUM(DECODE(T3.N_PROID,2,T3.N_AMOUNT,NULL)) AS SUM_CMY,
		SUM(DECODE(T3.N_PROID,1,T3.N_AMOUNT,0)+DECODE(T3.N_PROID,2,T3.N_AMOUNT,0)) AS SUM_USE,
		NULL AS REMARK
	FROM
		QS_PLANWEEK_DETAIL T3,
		QS_PLANWEEKAPPLY T4,
		SYS_COMPANY T5
	WHERE T3.N_WEEKAPP_ID = T4.N_WEEKAPP_ID
		AND T4.N_COMPANY_ID = T5.CMY_ID
		AND T4.N_COMPANY_ID IN({0})
		AND T4.N_YEAR = {1}
		AND T4.N_MONTH = {2}
		AND T4.N_WEEK = {3}
		GROUP BY T4.N_COMPANY_ID,T5.CMY_NAME
		ORDER BY T4.N_COMPANY_ID) TT,QS_PLANWEEKAPPLY T1
WHERE T1.N_COMPANY_ID=TT.N_COMPANY_ID
AND T1.N_COMPANY_ID IN({0})
AND T1.N_YEAR = {1}
AND T1.N_MONTH = {2}
AND T1.N_WEEK = {3}", cmyIds, year, month, week);
            var dt = weekBll.Query(sql);
            var row = dt.NewRow();
            row["CMY_NAME"] = "合计";
            row["SUM_PLAN"] = dt.AsEnumerable().Sum(a => a["SUM_PLAN"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_PLAN"]));
            row["SUM_ONE"] = dt.AsEnumerable().Sum(a => a["SUM_ONE"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_ONE"]));
            row["SUM_TWO"] = dt.AsEnumerable().Sum(a => a["SUM_TWO"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_TWO"]));
            row["SUM_THREE"] = dt.AsEnumerable().Sum(a => a["SUM_THREE"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_THREE"]));
            row["SUM_FOUR"] = dt.AsEnumerable().Sum(a => a["SUM_FOUR"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_FOUR"]));
            row["SUM_FIVE"] = dt.AsEnumerable().Sum(a => a["SUM_FIVE"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_FIVE"]));
            row["SUM_SIX"] = dt.AsEnumerable().Sum(a => a["SUM_SIX"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_SIX"]));
            row["SUM_SEVEN"] = dt.AsEnumerable().Sum(a => a["SUM_SEVEN"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_SEVEN"]));
            row["SUM_EIGHT"] = dt.AsEnumerable().Sum(a => a["SUM_EIGHT"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_EIGHT"]));
            row["SUM_NINE"] = dt.AsEnumerable().Sum(a => a["SUM_NINE"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_NINE"]));
            row["SUM_TEN"] = dt.AsEnumerable().Sum(a => a["SUM_TEN"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_TEN"]));
            row["SUM_ELEVEN"] = dt.AsEnumerable().Sum(a => a["SUM_ELEVEN"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_ELEVEN"]));
            row["SUM_TWELVE"] = dt.AsEnumerable().Sum(a => a["SUM_TWELVE"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_TWELVE"]));
            row["SUM_THIRTEEN"] = dt.AsEnumerable().Sum(a => a["SUM_THIRTEEN"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_THIRTEEN"]));
            row["SUM_CMY"] = dt.AsEnumerable().Sum(a => a["SUM_CMY"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_CMY"]));
            row["SUM_USE"] = dt.AsEnumerable().Sum(a => a["SUM_USE"] == DBNull.Value ? 0 : Convert.ToDecimal(a["SUM_USE"]));
            dt.Rows.Add(row);

            #endregion datatable SQL语句

            if (dt.Rows.Count > 1)
            {
                string minday = DateTime.Parse(dt.Rows[0]["D_START"].ToString()).Day.ToString();
                string maxday = DateTime.Parse(dt.Rows[0]["D_END"].ToString()).Day.ToString();
                this.lblDate.InnerText = dt.Rows[0]["N_YEAR"].ToString() + "年" + dt.Rows[0]["N_MONTH"].ToString() + "月" + minday + "-" + maxday + "日";
            }
            else
            {
                this.lblDate.InnerText = "yyyy年MM月dd-dd日";
            }

            //后台绑定
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            #region 分页

            //创建数据源
            PagedDataSource page = new PagedDataSource();
            page.DataSource = ds.Tables[0].DefaultView;
            //允许分页
            page.AllowPaging = true;
            //设置每页显示记录数
            page.PageSize = 10000;
            ////显示总共页数
            //lblTotalPage.Text = page.PageCount.ToString();
            ////当前页
            //int CurrentPage;
            ////请求页码为不为null设置当前页，否则为第一页
            //if (Request.QueryString["Page"] != null)
            //{
            //    CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
            //}
            //else
            //{
            //    CurrentPage = 1;
            //}
            ////当前页所引为页码-1
            //page.CurrentPageIndex = CurrentPage - 1;
            ////显示当前页码
            //lblCurrentPage.Text = CurrentPage.ToString();
            ////如果不是第一页，通过参数Page设置上一页为当前页-1，否则不显示连接
            //if (!page.IsFirstPage)
            //{
            //    //Request.CurrentExecutionFilePath为当前请求虚拟路径
            //    lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
            //}
            ////End If
            ////如果不是最后一页，通过参数Page设置下一页为当前页+1，否则不显示连接
            //if (!page.IsLastPage)
            //{
            //    //Request.CurrentExecutionFilePath为当前请求虚拟路径
            //    lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
            //}
            Repeater1.DataSource = page;

            #endregion 分页

            Repeater1.DataBind();
        }

        protected void QUERY_CLICK(object sender, EventArgs e)
        {
            BindDataSet();
        }

        protected void btnDaochu_Click(object sender, EventArgs e)
        {
            var execll = new BasePage();
            execll.toExecl1(Panel1, "周资金计划汇总表" + DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Millisecond.ToString());
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            var key = Guid.NewGuid().ToString();
            Session[key] = FORM_PRINT_VALUE.Value;
            MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=HTML&VALUE=" + key + "\",Area:[\"1000px\",\"500px\"],Scrollbar:true})");
        }
    }
}